//
//  TAMTNewChatController.swift
//  TAMeetApp
//
//  Created by 九月 on 12/5/24.
//

import UIKit

class TAMTNewChatController: UIViewController, UICollectionViewDelegate, UICollectionViewDataSource{
    
    private lazy var listCollectionView: UICollectionView = {
        let layout = UICollectionViewFlowLayout()
        layout.itemSize = CGSize(width: self.view.width, height: 70)
        layout.scrollDirection = .vertical
        layout.minimumLineSpacing = 12
        layout.sectionInset = UIEdgeInsets(top: 0, left: 0, bottom: 20, right: 0)

        let collectionView = UICollectionView(frame: .zero, collectionViewLayout: layout)
        collectionView.contentInsetAdjustmentBehavior = .never
        collectionView.backgroundColor = UIColor(hex: "#F2F2F6")
        collectionView.delegate = self
        collectionView.dataSource = self
        
        collectionView.register(TAMTTextNewsCell.self, forCellWithReuseIdentifier: "text.cell")
        collectionView.register(TAMTImageNewsCell.self, forCellWithReuseIdentifier: "image.cell")
        collectionView.showsVerticalScrollIndicator = false
        collectionView.showsHorizontalScrollIndicator = false
        return collectionView
    }()
    
    override func viewWillAppear(_ animated: Bool) {
        super.viewWillAppear(animated)
        
        self.navigationController?.setNavigationBarHidden(false, animated: true)
//    https://result.eolink.com/pbpvFEK3621e972473e4d633d6f07cac62524668faa11dd?uri=/news/top/square
    }
    
    var dataSources:[TAMTMeeage] = []

    override func viewDidLoad() {
        super.viewDidLoad()

        self.navigationItem.title = "九月"
        view.backgroundColor = .white
        
        dataSources = TAMTMeeage.defuatMessages()
        
        view.addSubview(listCollectionView)
        listCollectionView.snp.makeConstraints { make in
            make.top.equalTo(view.safeAreaLayoutGuide.snp.top)
            make.bottom.equalTo(view.safeAreaLayoutGuide.snp.bottom)
            make.leading.trailing.equalToSuperview()
        }
        
        
    }

    func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
        return dataSources.count// 根据需求调整
    }

    func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
        let message = dataSources[indexPath.row]
        if message.messageType == .text {
            guard let cell = collectionView.dequeueReusableCell(
                withReuseIdentifier: String(describing: "text.cell"),
                for: indexPath
            ) as? TAMTTextNewsCell else {
                fatalError("Unable to dequeue YOUfanGoddessGridsCell")
            }
            // 配置 cell
            return cell
        }
        else if (message.messageType == .image) {
            guard let cell = collectionView.dequeueReusableCell(
                withReuseIdentifier: String(describing: "image.cell"),
                for: indexPath
            ) as? TAMTImageNewsCell else {
                fatalError("Unable to dequeue YOUfanGoddessGridsCell")
            }
            // 配置 cell
            return cell
        }
        else {
            guard let cell = collectionView.dequeueReusableCell(
                withReuseIdentifier: String(describing: "text.cell"),
                for: indexPath
            ) as? TAMTTextNewsCell else {
                fatalError("Unable to dequeue YOUfanGoddessGridsCell")
            }
            // 配置 cell
            return cell
        }
    }

    // MARK: - UICollectionView Delegate Methods
    func collectionView(_ collectionView: UICollectionView, didSelectItemAt indexPath: IndexPath) {
        print("Selected item at \(indexPath)")
    }
    
    func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, sizeForItemAt indexPath: IndexPath) -> CGSize {
        return CGSize(width: view.width, height: 200)
    }
}

